//
//  HMAddCartView.m
//  HMMarket
//
//  Created by maimaigo3 on 2017/5/18.
//  Copyright © 2017年 homai. All rights reserved.
//
#define height 49
#define btnWidthAndHeight 37
#define Marin 7
#define addCartWidth 120
#import "HMAddCartView.h"

@interface HMAddCartView ()<UITextFieldDelegate>

NS_STRONG UIButton *reduceBtn;

NS_STRONG UILabel *numberOfShopPro;

@end

@implementation HMAddCartView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setUpSubView];
    }
    return self;
}

- (void)setUpSubView {
    self.backgroundColor = [UIColor whiteColor];
    
    UIButton *cartBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    cartBtn.layer.cornerRadius = 45/2;
    cartBtn.clipsToBounds = YES;
    [cartBtn setBackgroundImage:[UIImage imageNamed:@"ProductList_shopcart"] forState:UIControlStateNormal];
    cartBtn.frame = CGRectMake(15, -15, 45, 45);
    [cartBtn addTarget:self action:@selector(pushToCartVC) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:cartBtn];
    
    _numberOfShopPro = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(cartBtn.frame) - 20, cartBtn.frame.origin.y, 20, 20)];
    _numberOfShopPro.textColor = [UIColor whiteColor];
    _numberOfShopPro.text = [NSString stringWithFormat:@"%ld", _numberOfCart];
    _numberOfShopPro.textAlignment = NSTextAlignmentCenter;
    _numberOfShopPro.layer.cornerRadius = 10;
    _numberOfShopPro.clipsToBounds = YES;
    _numberOfShopPro.font = [UIFont systemFontOfSize:12.0f];
    _numberOfShopPro.backgroundColor = [UIColor redColor];
    [self addSubview:_numberOfShopPro];
    
    _reduceBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_reduceBtn setBackgroundImage:[UIImage imageNamed:@"Cart_reduce_low"] forState:UIControlStateNormal];
    _reduceBtn.frame = CGRectMake(CGRectGetMaxX(cartBtn.frame) + 10,  6, btnWidthAndHeight, btnWidthAndHeight);
    [_reduceBtn addTarget:self action:@selector(reduceNumber) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_reduceBtn];
    
    _numberTextField = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMaxX(_reduceBtn.frame) + Marin, 6, btnWidthAndHeight, btnWidthAndHeight)];
    _numberTextField.delegate = self;
    _numberTextField.textAlignment = NSTextAlignmentCenter;
    _numberTextField.text = @"1";
    _numberTextField.keyboardType = UIKeyboardTypeNamePhonePad;
    [self addSubview:_numberTextField];
    
    UIButton *addBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [addBtn setBackgroundImage:[UIImage imageNamed:@"Cart_add_hightlight"] forState:UIControlStateNormal];
    addBtn.frame = CGRectMake(CGRectGetMaxX(_numberTextField.frame) + Marin, 6, btnWidthAndHeight, btnWidthAndHeight);
    [addBtn addTarget:self action:@selector(addNumber) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:addBtn];
    
    UIButton *addCart = [UIButton buttonWithType:UIButtonTypeCustom];
    addCart.titleLabel.font = [UIFont systemFontOfSize:16.0f];
    [addCart setTitle:@"加入购物车" forState:UIControlStateNormal];
    [addCart addTarget:self action:@selector(addCart) forControlEvents:UIControlEventTouchUpInside];
    [addCart setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    addCart.frame = CGRectMake(KWidthSCREEN - addCartWidth, 0, addCartWidth, height);
    [addCart setBackgroundColor:kMainColor];
    [self addSubview:addCart];
}

- (void)reduceNumber {
    if ([MGAccountTool getTel].length == 0) {
        if ([_delegate respondsToSelector:@selector(noLogin)]) {
            [_delegate noLogin];
        }
    } else {
        if ([_numberTextField.text integerValue] > 1) {
            NSInteger number = [_numberTextField.text integerValue];
            number --;
            _numberTextField.text = [NSString stringWithFormat:@"%ld", number];
            if (number == 1) {
                [_reduceBtn setBackgroundImage:[UIImage imageNamed:@"Cart_reduce_low"] forState:UIControlStateNormal];
            }
        }
    }
    
    
    
}

- (void)addNumber {
    if ([MGAccountTool getTel].length == 0) {
        if ([_delegate respondsToSelector:@selector(noLogin)]) {
            [_delegate noLogin];
        }
    } else {
    NSInteger number = [_numberTextField.text integerValue];
    number ++;
    if (number == 1) {
        [_reduceBtn setBackgroundImage:[UIImage imageNamed:@"Cart_reduce_low"] forState:UIControlStateNormal];
    } else {
        [_reduceBtn setBackgroundImage:[UIImage imageNamed:@"Cart_reduce_hightlight"] forState:UIControlStateNormal];
    }
    _numberTextField.text = [NSString stringWithFormat:@"%ld", number];
    }
}

- (void)addCart {
    if ([_delegate respondsToSelector:@selector(addCart)]) {
        [_delegate addCart];
    }
}

- (void )setNumberOfCart:(NSInteger)numberOfCart {
//    NSInteger number = [_numberOfShopPro.text integerValue];
//    number ++;
    _numberOfCart = numberOfCart;
    _numberOfShopPro.text = [NSString stringWithFormat:@"%ld", _numberOfCart];
}

- (void)pushToCartVC {
    if ([MGAccountTool getTel].length == 0) {
        if ([_delegate respondsToSelector:@selector(noLogin)]) {
            [_delegate noLogin];
        }
    } else {
        if ([_delegate respondsToSelector:@selector(pushToCartVC)]) {
            [_delegate pushToCartVC];
        }
    }
}

#pragma mark - UITextFieldDelegate
- (void)textFieldDidEndEditing:(UITextField *)textField {
    if ([textField.text integerValue] < 1) {
        [HMHUDManager showHUDWithText:@"最少为1"];
        textField.text = @"1";
    }
}
@end
